package com.joker.web.controller.orange;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.BuyOrderVo;
import com.joker.orange.domain.bo.BuyOrderBo;
import com.joker.orange.service.IBuyOrderService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 购买订单
 *
 * @author Joker
 * @date 2024-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/consumer/buy")
public class BuyOrderController extends BaseController {

    private final IBuyOrderService iBuyOrderService;

    /**
     * 查询购买订单列表
     */
    @SaCheckPermission("consumer:buy:list")
    @GetMapping("/list")
    public TableDataInfo<BuyOrderVo> list(BuyOrderBo bo, PageQuery pageQuery) {
        return iBuyOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出购买订单列表
     */
    @SaCheckPermission("consumer:buy:export")
    @Log(title = "购买订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BuyOrderBo bo, HttpServletResponse response) {
        List<BuyOrderVo> list = iBuyOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "购买订单", BuyOrderVo.class, response);
    }

    /**
     * 获取购买订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("consumer:buy:query")
    @GetMapping("/{id}")
    public R<BuyOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBuyOrderService.queryById(id));
    }

    /**
     * 新增购买订单
     */
    @SaCheckPermission("consumer:buy:add")
    @Log(title = "购买订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BuyOrderBo bo) {
        return toAjax(iBuyOrderService.insertByBo(bo));
    }

    /**
     * 修改购买订单
     */
    @SaCheckPermission("consumer:buy:edit")
    @Log(title = "购买订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BuyOrderBo bo) {
        return toAjax(iBuyOrderService.updateByBo(bo));
    }

    /**
     * 删除购买订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("consumer:buy:remove")
    @Log(title = "购买订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBuyOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
